package com.np.shop.controller.lgs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.np.shop.entity.TbSpecification;
import com.np.shop.entity.TbSpecificationOption;
import com.np.shop.service.ITbSpecificationOptionService;
import com.np.shop.service.ITbSpecificationService;
import com.np.shop.utils.PageResult;
import com.np.shop.utils.Result;
import com.np.shop.utils.StatusCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李高帅
 * @since 2020-11-27
 */

@CrossOrigin
@RestController
@RequestMapping("/specification")
public class TbSpecificationController {

    @Resource
    private ITbSpecificationService iTbSpecificationService;

    @Resource
    private ITbSpecificationOptionService iTbSpecificationOptionService;
    /**
     * 分页+列表+模糊
     * @param map
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/list/{page}/{size}")
    public Result listMap(@RequestBody Map map,@PathVariable Integer page,@PathVariable  Integer size){
        PageResult<TbSpecification> pageResult=iTbSpecificationService.selectList(map,page,size);
        return new Result(StatusCode.SUCCESS,true,"Ok",pageResult);
    }

    /**
     * 添加规格
     * @param tbSpecification
     * @return
     */
    @PostMapping("/addSpecification")
    public Result addSpecification(@RequestBody TbSpecification tbSpecification){
        iTbSpecificationService.addSpecification(tbSpecification);
        return new Result(StatusCode.SUCCESS,true,"Ok",null);
    }

    /**
     * 查询  通过id进行回显
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        //查询当前的 主表
        TbSpecification tbSpecification=iTbSpecificationService.getById(id);

        QueryWrapper<TbSpecificationOption>   queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TbSpecificationOption::getSpecId,id);
        List<TbSpecificationOption> tbSpecificationOptions=iTbSpecificationOptionService.list(queryWrapper);
        tbSpecification.setTbSpecificationOptions(tbSpecificationOptions);

        return new Result(StatusCode.SUCCESS,true,"Ok",tbSpecification);
    }

}
